In [1]:
import imutils 
import cv2
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
from colorspacious import cspace_converter
In [2]:
img_grayscale = cv2.imread('C:\\Users\\rishitha\\Downloads\\bts-family-2.jpg',0)
img_color = cv2.imread('C:\\Users\\rishitha\\Downloads\\bts-family-2.jpg', 1)
img_uc = cv2.imread('C:\\Users\\rishitha\\Downloads\\bts-family-2.jpg', -1)
In [3]:
img_uc.shape #(width,height,depth)
Out[3]:
(1335, 2000)
In [4]:
cv2.imshow('grayscale image',img_grayscale)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('C:\\Users\\rishitha\\Downloads\\bts-family-2.jpg',img_grayscale)
Out[4]:
True
In [5]:
cv2.imshow('color image',img_color)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('C:\\Users\\rishitha\\Downloads\\bts-family-2.jpg',img_color)
Out[5]:
True
In [6]:
cv2.imshow('uc image',img_uc)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('C:\\Users\\rishitha\\Downloads\\bts-family-2.jpg',img_uc)
Out[6]:
True
In [27]:
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=[20,20])
ax[0].imshow(img_grayscale)
ax[1].imshow(img_color)
ax[2].imshow(img_uc)
fig.tight_layout()
plt.show()
In [7]:
import cv2
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
cmaps = {}
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
def plot_color_gradients(category, cmap_list):
 # Create figure and adjust figure height to number of colormaps
    nrows = len(cmap_list)
    figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22
    fig, axs = plt.subplots(nrows=nrows + 1, figsize=(6.4, figh))
    fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
                        left=0.2, right=0.99)
    axs[0].set_title(f'{category} colormaps', fontsize=14)
    for ax, name in zip(axs, cmap_list):
        ax.imshow(gradient, aspect='auto', cmap=mpl.colormaps[name])
        ax.text(-0.01, 0.5, name, va='center', ha='right', fontsize=10,
                transform=ax.transAxes)
 # Turn off *all* ticks & spines, not just the ones with colormaps.
    for ax in axs:
        ax.set_axis_off()
 # Save colormap list for later.
    cmaps[category] = cmap_list
In [8]:
plot_color_gradients('Perceptually Uniform Sequential', ['viridis', 'plasma', 'inferno', 'magma', 'cividis'])
In [9]:
plot_color_gradients('Sequential',['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds','YlOrBr', 'YlOrRd',
'OrRd', 'PuRd', 'RdPu', 'BuPu', 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn'])
In [10]:
import cv2
import matplotlib as mpl
from matplotlib import pyplot as plt
img = cv2.imread('C:\\Users\\rishitha\\Downloads\\143.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#PLOTTING COLOR CHANNELS
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=[10,10])
ax[0].imshow(img *[1,0,0])
ax[0].set_title('REDS')
ax[1].imshow(img *[0,1,0])
ax[1].set_title('GREENS')
ax[2].imshow(img*[0,0,1])
ax[2].set_title('BLUES')
fig.tight_layout()
plt.show()
#SPLITTING IMAGE INTO COLOR CHANNELS
r,g,b = cv2.split(img)
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=[10,10])
ax[0].imshow(r)
ax[0].set_title('REDS')
ax[1].imshow(g)
ax[1].set_title('GREENS')
ax[2].imshow(b)
ax[2].set_title('BLUES')
fig.tight_layout()
plt.show()
In [11]:
import cv2
import matplotlib as mpl
from matplotlib import pyplot as plt
img = cv2.imread('C:\\Users\\rishitha\\Downloads\\th.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#HISTOGRAM WITHOUT SPLITTING THE CHANNELS
colors = ('r','g','b')

# compute and plot the image histograms
for i,color in enumerate(colors):
    hist = cv2.calcHist([img],[i],None,[256],[0,256])
    plt.plot(hist,color = color)
    plt.title('Image Histogram')
    plt.show()
#HISTOGRAM AFTER CHANNEL SPLITTING
r,g,b = cv2.split(img)
print("The shape of img is " + str(img.shape))
print("The shape of r is " + str(r.shape))
print("The shape of g is " + str(g.shape))
print("The shape of b is " + str(b.shape))
fig, ax = plt.subplots(nrows=3, ncols=2, figsize=[10,10])
ax[0][0].imshow(b, cmap='Blues')
ax[0][0].set_title('BLUE IMAGE')
ax[0][1].hist(b.ravel(),256,[0,256])
ax[1][0].imshow(g, cmap='Greens')
ax[1][0].set_title('GREEN IMAGE')
ax[1][1].hist(g.ravel(),256,[0,256])
ax[2][0].imshow(r, cmap='Reds')
ax[2][0].set_title('RED IMAGE')
ax[2][1].hist(r.ravel(),256,[0,256])
fig.tight_layout()
plt.show()
The shape of img is (180, 288, 3)
The shape of r is (180, 288)
The shape of g is (180, 288)
The shape of b is (180, 288)
In [12]:
import cv2
import matplotlib as mpl
from matplotlib import pyplot as plt
img = cv2.imread('C:\\Users\\rishitha\\Downloads\\123.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
r,g,b = cv2.split(img)
#EQUALIZING HISTOGRAM
eb = cv2.equalizeHist(b)
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=[10,10])
ax[0][0].imshow(b, cmap='Blues')
ax[0][0].set_title('ORIGINAL BLUE IMAGE')
ax[0][1].hist(b.ravel(),256,[0,256])
ax[1][0].imshow(eb, cmap='Blues')
ax[1][0].set_title('EQUALIZED IMAGE')
ax[1][1].hist(eb.ravel(),256,[0,256])
plt.show()
er = cv2.equalizeHist(r)
eg = cv2.equalizeHist(g)
#MERGING CHANNELS
eqim = cv2.merge([er, eg, eb])
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=[10,10])
ax[0].imshow(img)
ax[0].set_title('ORIGINAL IMAGE')
ax[1].imshow(eqim)
ax[1].set_title('EQUALIZED IMAGE')
plt.show()
In [13]:
import cv2
import matplotlib as mpl
from matplotlib import pyplot as plt
img = cv2.imread('C:\\Users\\rishitha\\Downloads\\143.jpeg')
im1 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
im1_yuv = cv2.cvtColor(im1,cv2.COLOR_BGR2YUV)
im1_yuv[:,:,0] = cv2.equalizeHist(im1_yuv[:,:,0])
hist_eq = cv2.cvtColor(im1_yuv, cv2.COLOR_YUV2BGR)
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=[25,25])
ax[0].imshow(im1)
ax[0].set_title('ORIGINAL IMAGE')
ax[1].imshow(hist_eq)
ax[1].set_title('EQUALIZING \nUSING YUV MODEL')
ax[2].imshow(eqim)
ax[2].set_title('EQUALIZING \nEACH CHANNEL')
plt.show()
In [14]:
import cv2
import matplotlib as mpl
from matplotlib import pyplot as plt
li = cv2.imread('C:\\Users\\rishitha\\Downloads\\low-light-photography-featured.jpg',0)
#li_yuv = cv2.cvtColor(li,cv2.COLOR_BGR2YUV)
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=[10,10])
ax[0].imshow(li, cmap='gray')
ax[0].set_title("Original")
li = cv2.equalizeHist(li)
ax[1].set_title("Enhanced")
ax[1].imshow(li, cmap='gray')
plt.show()
In [15]:
import cv2
import matplotlib as mpl
from matplotlib import pyplot as plt
img = cv2.imread('C:\\Users\\rishitha\\Downloads\\143.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
r,g,b = cv2.split(img)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(15,15))
ceb = clahe.apply(b)
ceg = clahe.apply(g)
cer = clahe.apply(r)
ceqim = cv2.merge([cer,ceg,ceb])
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=[15,15])
ax[0].imshow(img)
ax[0].set_title('ORIGINAL IMAGE')
ax[1].imshow(ceqim)
ax[1].set_title('LOCAL EQUALIZED IMAGE')
ax[2].imshow(ceqim) #this is taken from expt 4
ax[2].set_title('EQUALIZED IMAGE')
plt.show()
In [16]:
import cv2
import matplotlib as mpl
from matplotlib import pyplot as plt
img = cv2.imread('C:\\Users\\rishitha\\Downloads\\143.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
im1_yuv_clahe = cv2.cvtColor(img,cv2.COLOR_BGR2YUV)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(15,15))
im1_yuv_clahe[:,:,0] = clahe.apply(im1_yuv_clahe[:,:,0])
hist_eq_clahe = cv2.cvtColor(im1_yuv_clahe, cv2.COLOR_YUV2BGR)
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=[15,15])
ax[0].imshow(img)
ax[0].set_title('ORIGINAL IMAGE')
ax[1].imshow(hist_eq_clahe)
ax[1].set_title('LOCAL EQUALIZED \n YUV')
ax[2].imshow(ceqim) #Taken from previous program
ax[2].set_title('LOCAL EQUALIZED \n EACH CHANNEL')
plt.show()
In [17]:
import cv2
import matplotlib as mpl
from matplotlib import pyplot as plt
li0 = cv2.imread('C:\\Users\\rishitha\\Downloads\\low-light-photography-featured.jpg',0)
#li_yuv = cv2.cvtColor(li,cv2.COLOR_BGR2YUV)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(15,15))
fig, ax = plt.subplots(nrows=2, ncols=3, figsize=[10,10])
ax[0][0].imshow(li0, cmap='gray')
li1 = cv2.equalizeHist(li0)
ax[0][1].imshow(li1, cmap='gray')
li2 = clahe.apply(li0)
ax[0][2].imshow(li1, cmap='gray')
ax[1][0].hist(li0.ravel(),256,[0,256])
ax[1][1].hist(li1.ravel(),256,[0,256])
ax[1][2].hist(li2.ravel(),256,[0,256])
fig.tight_layout()
plt.show()
In [18]:
pip install scikit-image
Requirement already satisfied: scikit-image in c:\users\rishitha\anaconda3\lib\site-packages (0.19.2)
Requirement already satisfied: scipy>=1.4.1 in c:\users\rishitha\anaconda3\lib\site-packages (from scikit-image) (1.7.3)
Requirement already satisfied: tifffile>=2019.7.26 in c:\users\rishitha\anaconda3\lib\site-packages (from scikit-image) (2021.7.2)
Requirement already satisfied: imageio>=2.4.1 in c:\users\rishitha\anaconda3\lib\site-packages (from scikit-image) (2.9.0)
Requirement already satisfied: PyWavelets>=1.1.1 in c:\users\rishitha\anaconda3\lib\site-packages (from scikit-image) (1.3.0)
Requirement already satisfied: packaging>=20.0 in c:\users\rishitha\anaconda3\lib\site-packages (from scikit-image) (21.3)
Requirement already satisfied: networkx>=2.2 in c:\users\rishitha\anaconda3\lib\site-packages (from scikit-image) (2.7.1)
Requirement already satisfied: pillow!=7.1.0,!=7.1.1,!=8.3.0,>=6.1.0 in c:\users\rishitha\anaconda3\lib\site-packages (from scikit-image) (9.0.1)
Requirement already satisfied: numpy>=1.17.0 in c:\users\rishitha\anaconda3\lib\site-packages (from scikit-image) (1.21.5)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in c:\users\rishitha\anaconda3\lib\site-packages (from packaging>=20.0->scikit-image) (3.0.4)
Note: you may need to restart the kernel to use updated packages.
In [19]:
import cv2
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
from skimage import exposure
from skimage.exposure import match_histograms
src = cv2.imread('C:\\Users\\rishitha\\Downloads\\fruits-png-image-fruits-png-image-download-39.png')
src_yuv = cv2.cvtColor(src,cv2.COLOR_BGR2YUV)
ref = cv2.imread('C:\\Users\\rishitha\\Downloads\\fruit.jpeg')
ref = cv2.cvtColor(ref,cv2.COLOR_BGR2RGB)
ref = cv2.resize(ref, (src.shape[1], src.shape[0]))
ref_yuv = cv2.cvtColor(ref,cv2.COLOR_BGR2YUV)
print('Shape of src image = ' + str(src.shape))
print('Shape of ref image = ' + str(ref.shape))
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=[15,15])
ax[0].imshow(src)
ax[0].set_title('ORIGINAL IMAGE')
ax[1].imshow(ref)
ax[1].set_title('REFERENCE MATCHED')
plt.show()
src_yuv[:,:,0] = match_histograms(src_yuv[:,:,0], ref_yuv[:,:,0])
src1 = cv2.cvtColor(src_yuv,cv2.COLOR_YUV2BGR)
src2 = match_histograms(src, ref, multichannel=True)
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=[15,15])
ax[0].imshow(src)
ax[0].set_title('ORIGINAL IMAGE')
ax[1].imshow(src1)
ax[1].set_title('HISTOGRAM MATCHED\nIN YUV MODEL')
ax[2].imshow(src2)
ax[2].set_title('COLOR HISTOGRAM MATCHED')
plt.show()
Shape of src image = (1200, 1200, 3)
Shape of ref image = (1200, 1200, 3)
C:\Users\rishitha\AppData\Local\Temp\ipykernel_16128\38739510.py:23: FutureWarning: `multichannel` is a deprecated argument name for `match_histograms`. It will be removed in version 1.0. Please use `channel_axis` instead.
  src2 = match_histograms(src, ref, multichannel=True)
In [20]:
import cv2
import matplotlib as mpl
from matplotlib import pyplot as plt
img = cv2.imread('C:\\Users\\rishitha\\Downloads\\puppy.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
ns1 = (640, 480)
ri1 = cv2.resize(img, ns1)
ri2 = cv2.resize(img, (0,0), fx=0.25, fy=0.7, interpolation = cv2.INTER_AREA)
fig,ax = plt.subplots(nrows=1, ncols=3, figsize=[10,10])
ax[0].imshow(img)
ax[0].set_title('Original Image\nShape: ' + str(ri2.shape))
ax[1].imshow(ri1)
ax[1].set_title('Resized Image\nShape: ' + str(ri1.shape))
ax[2].imshow(ri2)
ax[2].set_title('Resized Image\nShape: ' + str(ri2.shape))
fig.tight_layout()
plt.show()
In [21]:
import cv2
import matplotlib as mpl
from matplotlib import pyplot as plt
img = cv2.imread('C:\\Users\\rishitha\\Downloads\\143.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
rot1 = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
rot2 = cv2.rotate(img, cv2.ROTATE_180)
rot3 = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
fig, ax = plt.subplots(nrows=1, ncols=4, figsize=[10,10])
ax[0].imshow(img)
ax[0].set_title('UNROTATED IMAGE')
ax[1].imshow(rot1)
ax[1].set_title('ROTATE_90_CLOCKWISE')
ax[2].imshow(rot2)
ax[2].set_title('ROTATE_180')
ax[3].imshow(rot3)
ax[3].set_title('ROTATE_90_COUNTERCLOCKWISE')
fig.tight_layout()
plt.show()
In [22]:
import cv2
import matplotlib as mpl
from matplotlib import pyplot as plt
import imutils
img = cv2.imread('C:\\Users\\rishitha\\Downloads\\143.jpeg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#INVERTING IMAGES
img_invt = cv2.bitwise_not(img)
#UNBOUNDED ROTATION
rot1 = imutils.rotate(img_invt, 45)
rot2 = imutils.rotate(img_invt, -45)
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=[10,10])
ax[0].imshow(img_invt)
ax[0].set_title('UNROTATED IMAGE')
ax[1].imshow(rot1)
ax[1].set_title('ROTATE 45°')
ax[2].imshow(rot2)
ax[2].set_title('ROTATE -45°')
fig.tight_layout()
plt.show()
#BOUNDED ROTATION
rot1 = imutils.rotate_bound(img_invt, 45)
rot2 = imutils.rotate_bound(img_invt, -45)
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=[10,10])
ax[0].imshow(img_invt)
ax[0].set_title('UNROTATED IMAGE')
ax[1].imshow(rot1)
ax[1].set_title('ROTATE 45°')
ax[2].imshow(rot2)
ax[2].set_title('ROTATE -45°')
fig.tight_layout()
plt.show()
In [23]:
import cv2
import matplotlib as mpl
from matplotlib import pyplot as plt
im1 = cv2.imread('C:\\Users\\rishitha\\Downloads\\th (1).jpeg')
im1=cv2.cvtColor(im1, cv2.COLOR_BGR2RGB)
im2 = cv2.imread('C:\\Users\\rishitha\\Downloads\\th.jpeg')
im2=cv2.cvtColor(im2, cv2.COLOR_BGR2RGB)
im2 = cv2.resize(im2, (im1.shape[1], im1.shape[0]))
im_add1 = cv2.addWeighted(im1, 0.75, im2, 0.25, 0)
im_add2 = cv2.add(im1, im2)
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=[10,10])
ax[0] [0].imshow(im1)
ax[0] [0].set_title('IMAGE 1')
ax[0] [1].imshow(im2)
ax[0] [1].set_title('IMAGE 2')
ax[1] [0].imshow(im_add1)
ax[1] [0].set_title('Weighted Addition')
ax[1] [1].imshow(im_add2)
ax[1] [1].set_title('Normal Addition')
fig.tight_layout()
plt.show()
In [24]:
import cv2
import matplotlib as mpl
from matplotlib import pyplot as plt
im1 = cv2.imread('C:\\Users\\rishitha\\Downloads\\th (1).jpeg')
im1=cv2.cvtColor(im1, cv2.COLOR_BGR2RGB)
im2 = cv2.imread('C:\\Users\\rishitha\\Downloads\\th.jpeg')
im2=cv2.cvtColor(im2, cv2.COLOR_BGR2RGB)
im2 = cv2.resize(im2, (im1.shape[1], im1.shape[0]))
im_sub = cv2.subtract(im1, im2)
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=[10,10])
ax[0].imshow(im1)
ax[0].set_title('IMAGE 1')
ax[1].imshow(im2)
ax[1].set_title('IMAGE 2')
ax[2].imshow(im_sub)
ax[2].set_title('IMAGES SUBTRACTED')
fig.tight_layout()
plt.show()
In [25]:
import cv2
import matplotlib as mpl
from matplotlib import pyplot as plt
im1 = cv2.imread('C:\\Users\\rishitha\\Downloads\\th.jpeg')
im1=cv2.cvtColor(im1, cv2.COLOR_BGR2RGB)
im2 = cv2.imread('C:\\Users\\rishitha\\Downloads\\th (1).jpeg')
im2=cv2.cvtColor(im2, cv2.COLOR_BGR2RGB)
im2 = cv2.resize(im2, (im1.shape[1], im1.shape[0]))
imor = cv2.bitwise_or(im1, im2, mask=None)
imand = cv2.bitwise_and(im1, im2, mask=None)
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=[10,10])
ax[0][0].imshow(im1)
ax[0][0].set_title('IMAGE 1')
ax[0][1].imshow(im2)
ax[0][1].set_title('IMAGE 2')
ax[1][0].imshow(imor)
ax[1][0].set_title('BITWISE OR')
ax[1][1].imshow(imand)
ax[1][1].set_title('BITWISE AND')
fig.tight_layout()
plt.show()
In [26]:
import cv2
import matplotlib as mpl
from matplotlib import pyplot as plt
im1 = cv2.imread('C:\\Users\\rishitha\\Downloads\\th (1).jpeg')
im1=cv2.cvtColor(im1, cv2.COLOR_BGR2RGB)
im2 = cv2.imread('C:\\Users\\rishitha\\Downloads\\rishi.jpeg')
im2=cv2.cvtColor(im2, cv2.COLOR_BGR2RGB)
im2 = cv2.resize(im2, (im1.shape[1], im1.shape[0]))
# MASK DEFINITION
# MASK TO BE SAME SIZE OF IMAGES
# THE PIXELS IN THE MASK WITH 0s WILL BE EXCLUDED IN FINAL IMAGE
# THE PIXELS WITH WHITE COLOR WITH 255s WILL BE INCLUDED
M1 = np.zeros(im1.shape[:2], dtype=np.uint8)
M1[50:300, 50:500] = 255
M1.astype(np.int8)
imor = cv2.bitwise_or(im1, im2, mask=M1)
imand = cv2.bitwise_and(im1, im2, mask=M1)
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=[10,10])
ax[0][0].imshow(im1)
ax[0][0].set_title('IMAGE 1')
ax[0][1].imshow(im2)
ax[0][1].set_title('IMAGE 2')
ax[1][0].imshow(imor)
ax[1][0].set_title('BITWISE OR')
ax[1][1].imshow(imand)
ax[1][1].set_title('BITWISE AND')
fig.tight_layout()
plt.show()
In [ ]: